const adminModel = require('../models/AdminModel')

let adminController = {
    /**
     * 列表渲染
     * @param {*} req 
     * @param {*} res 
     */
    get_showadmin: async function (req, res) {
        if (req.session.user == '') {
            res.redirect('/user/login')
        }

        
        let amdinData = await adminModel.check_admin(req, res);

        let userData = await adminModel.check_user(req, res);

        let textData = await adminModel.check_text(req, res);

        let commentData = await adminModel.check_comment(req, res);


        // console.log(amdinData, userData);
        // console.log(textData);
        // console.log(commentData);
        let interface = req.session.user.role;

        // console.log(interface);
        if (interface == 1) {
            this.fetch(req, res, { view: 'admin/showadmin', data: { amdinData, userData, textData, commentData, user: req.session.user } });

        } else if (interface == 2) {
            this.fetch(req, res, { view: 'admin/showtext', data: { textData, user: req.session.user } })
        } else if (interface == 3) {
            this.fetch(req, res, { view: 'admin/showcom', data: { commentData, user: req.session.user } })
        } else if (interface == 4) {
            this.fetch(req, res, { view: 'admin/showuser', data: { userData, user: req.session.user } })
        }
    },
    /**
     * 退出登录
     * @param {*} req 
     * @param {*} res 
     */

    get_logout: function (req, res) {

        req.session.destroy(err => {
            if (err) throw err;

            res.redirect('/user/login')
        })
    },

    /**
     * 用户密码重置
     * @param {*} req 
     * @param {*} res 
     */
    post_usort: function (req, res) {
        adminModel.rest_userpassword(req, res)
    },
    post_del: function (req, res) {

    },

    /**
     * 添加管理员页面跳转
     * @param {*} req 
     * @param {*} res 
     */

    get_adminadd: function (req, res) {

        this.fetch(req, res, {
            view: 'admin/adminadd', data: {
                user: req.query
            }
        })
    },

    /**
     * 添加管理员验证
     * @param {*} req 
     * @param {*} res 
     */
    post_adminadd: function (req, res) {

        adminModel.add_admin(req, res)
    },

    /**
     * 修改密码页面跳转
     * @param {*} req 
     * @param {*} res 
     */

    get_asort: function (req, res) {

        this.fetch(req, res, {
            view: 'admin/asort', data: {
                user: req.query
            }
        })
    },   

    /**
     * 密码修改验证
     * @param {*} req 
     * @param {*} res 
     */
    post_asort: function (req, res) {

        adminModel.res_adminpawd(req, res)
    },

    /**
     * 文章删除
     * @param {*} req 
     * @param {*} res 
     */
    post_textd: function (req, res) {
        adminModel.delet_text(req, res)
    },
    /**
     * 评论删除
     * @param {*} req 
     * @param {*} res 
     */
    post_comment: function (req, res) {

        adminModel.delet_com(req, res)
    }



}


adminController.__proto__ = require('../../common/controllers/BaseController');

module.exports = adminController